package org.example.netty.test.nio.netty.chat.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.bean.GetALLUserMessage;
import org.example.netty.test.nio.netty.chat.bean.User;
import org.example.netty.test.nio.netty.chat.session.GlobalContent;
import org.example.netty.test.nio.netty.chat.util.NettyChannelUtil;

import java.util.List;
import java.util.Set;

/**
 * @author zp
 * @version 1.0
 * @description: 处理GetAllUserMessage消息
 * @date 2023/4/16 17:28
 */
@Slf4j
public class GetALLUserMessageHandler extends SimpleChannelInboundHandler<GetALLUserMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GetALLUserMessage msg) throws Exception {
        Channel channel = ctx.channel();
        Set<String> userNames = GlobalContent.getInstance().getUserNames();
        // 获取自己的用户信息 并排除
        User userServer = NettyChannelUtil.getUserServer(channel);
        assert userServer != null;
        String userName = userServer.getUserName();
        log.debug("查询所有用户消息：{}",msg);
        userNames.remove(userName);
        msg.setAllUserChatNames(userNames);
        msg.setList(msg.isList());
        channel.writeAndFlush(msg);
    }


}
